//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Data Access Application Block
//===============================================================================

using EntLibContrib.Data.MySql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Data.MySql.Tests
{
    /// <summary>
    /// Tests the MySqlDatabase class by using it to connect to a MySQL
    /// database and executing a SQL statement and a stored procedure.
    /// </summary>
    /// 
    /// <author>Wesley Hobbie</author>
    /// <version>1.00</version> 12/27/2005
    [TestClass]
    public class MySqlDatabaseFixture
    {
        private const string BASE_DIRECTORY_QUERY =
                                               "SHOW VARIABLES LIKE 'basedir'";
        private const string ENCRYPT_PASSWORD_SP = "EncryptPassword";

        private MySqlDatabase _database;

        /// <summary>
        /// Initializes the database.
        /// </summary>
        [TestInitialize]
        public void Init()
        {
            this._database = DatabaseFactory.CreateDatabase() as MySqlDatabase;
        }

        /// <summary>
        /// Tests connecting to the database.
        /// </summary>
			[TestMethod]
        public void ConnectionTest()
        {
            // Get connection, assert that the connection is not null, open the
            // connection, assert that the connection is open, then close the
            // connection
            DbConnection dbConnection = this._database.CreateConnection();
            Assert.IsNotNull(dbConnection);
            Assert.IsTrue(dbConnection is MySqlConnection);
            dbConnection.Open();
            Assert.IsTrue(dbConnection.State == ConnectionState.Open);
            dbConnection.Close();
        }

        /// <summary>
        /// Tests executing a SQL statement.
        /// </summary>
			[TestMethod]
        public void ExecuteSqlStatement()
        {
            using (DbCommand dbCommand = this._database.GetSqlStringCommand(
                MySqlDatabaseFixture.BASE_DIRECTORY_QUERY))
            {
                using (IDataReader dataReader = this._database.ExecuteReader(dbCommand))
                {
                }
            }
        }

        /// <summary>
        /// Tests executing a stored procedure.
        /// </summary>
			[TestMethod]
        public void ExecuteStoredProcedure()
        {
            using (DbCommand dbCommand = this._database.GetStoredProcCommand(
                MySqlDatabaseFixture.ENCRYPT_PASSWORD_SP))
            {
                this._database.AddInParameter(dbCommand,
                    this._database.BuildParameterName("password"), DbType.String, "password");
                using (IDataReader dataReader = this._database.ExecuteReader(dbCommand))
                {
                }
            }
        }
    }
}